export const namespace = "adminSystemRole";
import { Popconfirm, Switch, Tag } from "antd";

export const calcColumns = (params) => {
  const { operate, openAllocatedModel, toggleState } = params;
  const columns = [
    {
      title: "角色名称",
      dataIndex: "roleName",
      render: (_, record) => {
        return (
          <span>
            {record.roleName}
            {record.isSystem == "1" && (
              <Tag style={{ marginLeft: "10px" }} color="processing">
                系统
              </Tag>
            )}
          </span>
        );
      },
    },
    {
      title: "用户数量",
      dataIndex: "userCount",
      hideInSearch: true,
      render: (_, record) => {
        return record.userCount > 0 ? (
          <a onClick={() => openAllocatedModel(record)}>{record.userCount}</a>
        ) : (
          0
        );
      },
    },
    {
      title: "状态",
      dataIndex: "status",
      hideInSearch: true,
      render: (id, record) => {
        return record.isSystem == "0" ? (
          <>
            {record.status == "0" ? (
              <Popconfirm
                title="禁用角色后,该角色下的用户需要重新登录"
                onConfirm={() => toggleState({ ...record, status: 1 })}
              >
                <Switch checkedChildren="启用" checked />
              </Popconfirm>
            ) : (
              <Switch
                onClick={() => {
                  toggleState({ ...record, status: 0 });
                }}
                checkedChildren="启用"
                unCheckedChildren="禁用"
              />
            )}
          </>
        ) : (
          <Switch disabled checkedChildren="启用" checked />
        );
      },
    },
  ];

  columns.push(operate);
  return columns;
};
export const MenutagList = {
  M: {
    color: "blue",
    text: "菜单",
  },
  C: {
    color: "cyan",
    text: "页面",
  },
  F: {
    color: "green",
    text: "按钮",
  },
};
export const flattenTree = (tree) => {
  let result = [];

  function flattenChildren(node) {
    // 将当前节点加入到结果数组中
    result.push(node.id);
    // 如果有子节点，递归平铺子节点
    if (node.children && node.children.length) {
      node.children.forEach((child) => flattenChildren(child));
    }
  }

  // 对树中的每个节点应用平铺操作
  tree.forEach((node) => flattenChildren(node));

  return result;
};

export const flattenTreeLabel = (tree) => {
  let result = {};

  function flattenChildren(node) {
    // 将当前节点加入到结果数组中
    result[node.id] = node.label;
    // 如果有子节点，递归平铺子节点
    if (node.children && node.children.length) {
      node.children.forEach((child) => flattenChildren(child));
    }
  }

  // 对树中的每个节点应用平铺操作
  tree.forEach((node) => flattenChildren(node));

  return result;
};

export const disableKeysInTree = (tree, keys) => {
  // 将keys数组转换成一个Set，以便快速查找
  const keysSet = new Set(keys);

  // 递归函数来处理每个节点，并返回深拷贝的节点
  function processNode(node) {
    // 创建节点的深拷贝
    const newNode = { ...node, children: node.children ? [] : undefined };

    // 检查当前节点的key是否在keys数组中
    if (keysSet.has(node.id)) {
      // 如果是，设置disabled属性为true
      newNode.disabled = true;
    } else {
      newNode.disabled = false;
    }

    // 如果节点有子节点，递归处理每个子节点
    if (node.children && node.children.length) {
      newNode.children = node.children.map(processNode);
    }

    return newNode;
  }

  // 对树中的每个节点应用processNode函数，并返回深拷贝的树结构
  return tree.map(processNode);
};
